﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 智信科技物流系统
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private const int CODELENGTH = 4;
        //随机码          
        private String randomCode = "";
    
        public string username;

        private string CreateRandomCode(int length)
        {
            int rand;
            char code;
            string randomCode = String.Empty;
            //生成一定长度的验证码              
            Random random = new Random();
            for (int i = 0; i < length; i++)
            {
                rand = random.Next();
                if (rand % 3 == 0)
                {
                    code = (char)('A' + (char)(rand % 26));
                }
                else
                {
                    code = (char)('0' + (char)(rand % 10));
                }
                randomCode += code.ToString();
            }
            return randomCode;
        }

        private void CreateImage(string randomCode)
        {
            try
            {
                int randAngle = 45;
                //随机旋转角度                 
                int mapWidth = (int)(randomCode.Length * 21);
                //存放验证码的图片的长度                 
                Bitmap map = new Bitmap(mapWidth, 28);
                //创建图片背景                  
                Graphics graph = Graphics.FromImage(map);
                graph.Clear(Color.AliceBlue);//清除画面，填充背景                  
                graph.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);
                //画一个边框                  
                graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                //指定模式                   
                Random rand = new Random();
                //背景噪点生成                  
                Pen blackPen = new Pen(Color.LightGray, 0);
                for (int i = 0; i < 20; i++)
                {
                    int x = rand.Next(0, map.Width);
                    int y = rand.Next(0, map.Height);
                    graph.DrawRectangle(blackPen, x, y, 1, 1);
                }                   //验证码旋转，防止机器识别                 
                char[] chars = randomCode.ToCharArray();//拆散字符串成单字符数组                  
                //文字居中                  
                StringFormat format = new StringFormat(StringFormatFlags.NoClip);
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                //定义颜色                  
                Color[] c = { Color.Black, Color.Blue, Color.Red, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan };
                //定义字体                  
                string[] font = { "Verdana", "Arial", "宋体" };
                for (int i = 0; i < chars.Length; i++)
                {
                    int cindex = rand.Next(6);
                    int findex = rand.Next(2);
                    Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);
                    //字体样式，参数2为字体大小                      
                    Brush b = new System.Drawing.SolidBrush(c[cindex]);
                    Point dot = new Point(16, 16);
                    float angle = rand.Next(-randAngle, +randAngle);//随机旋转的角度                    
                    graph.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置                  
                    graph.RotateTransform(angle);//旋转角度                     
                    graph.DrawString(chars[i].ToString(), f, b, 1, 1, format);
                    graph.RotateTransform(-angle);//转回去                     
                    graph.TranslateTransform(2, -dot.Y);//移动光标到指定位置                 
                }
                pic_AuthCode.Image = map;
            }
            catch (ArgumentException)
            {
                MessageBox.Show("创建图片错误！");
            }
        }

        private void updatePic()
        {
            randomCode = CreateRandomCode(CODELENGTH);
            CreateImage(randomCode);
        }


        private void Register_Load(object sender, EventArgs e)
        {
            updatePic();
            //SetSkinFile(Properties.Resources.深海浅蓝);
            pic_SurePassWord.Visible = false;
            pic_SureAuthCode.Visible = false;
        }

        private void btn_Register_Click(object sender, EventArgs e)
        {
            try
            {
                SqlHelper helper=new SqlHelper();
                string str = "select count(*) from bm_User where UserName=N'" + tb_Name.Text.Trim() + "'";
                int i = helper.SqlScalar(str);
                if (i > 0)
                {
                    lb_EnUserName.ForeColor = Color.Red;
                    lb_EnUserName.Text = "该用户已经存在";
                    updatePic();
                    return;
                }
                else
                {
                    if (tb_Name.Text.Length > 50 || tb_Name.Text == "")
                    {//内容超长

                        lb_EnUserName.ForeColor = Color.Red;
                        lb_EnUserName.Text = "用户名长度有误";
                        this.tb_Name.Focus();
                        updatePic();
                        return;
                    }
                    else
                    {
                        lb_EnUserName.Text = "";
                    }
                    if (tb_PassWord.Text.Length > 16 || tb_PassWord.Text.Length < 6)
                    {
                        lb_EnPassWord.ForeColor = Color.Red;
                        lb_EnPassWord.Text = "密码长度有误";
                        picPassWord.Visible = false;
                        updatePic();
                        return;
                    }
                    else
                    {
                        lb_EnPassWord.Text = "";
                        picPassWord.Visible = true;
                    }
                    if (tb_SurePassword.Text != tb_PassWord.Text)
                    {
                        lblRePassWord.ForeColor = Color.Red;
                        lblRePassWord.Text = "两次输入的密码不一致";
                        pic_SurePassWord.Visible = false;
                        updatePic();
                        return;
                    }
                    else
                    {
                        lblRePassWord.Text = "";
                        pic_SurePassWord.Visible = true;
                    }
                    if (randomCode.Equals(btn_SureAuthCode.Text))
                    {
                        lblYanzheng.Text = "";
                        pic_SureAuthCode.Visible = true;

                    }
                    else
                    {
                        lblYanzheng.ForeColor = Color.Red;
                        lblYanzheng.Text = "请重新输入验证码";
                        updatePic();
                        pic_SureAuthCode.Visible = false;
                        return;
                    }
                    DateTime now=new DateTime();                
                    try
                    {
                        now = Dao.DataStandardTime();
                    }
                    catch 
                    {
                        MessageBox.Show("网络异常，请尝试重新注册");
                        return;
                    }
                    DateTime later = now.AddMonths(1);//一个月的试用期
                    string sql = "Insert into bm_User (UserName,PassWord,Permissions,Suspended,Lasttime,Expirationtime,RegistrationTime) values ('"
                        + tb_Name.Text + "' ,'" + tb_PassWord.Text + "','0',N'是','" + now.ToString("yyyy-MM-dd HH:mm") + "','"
                        + later.ToString("yyyy-MM-dd HH:mm") + "','" + now.ToString("yyyy-MM-dd HH:mm") + "')";
                    
                    helper.SqlNomQuery(sql);
                    //operate.OperateData(sql);
                    //---清空所有信息----------------
                   
                    MessageBox.Show("注册成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                  //  updatePic();

                }
            }
            catch
            {
                MessageBox.Show("注册失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                updatePic();
            }
        }

        private void btn_NextOne_Click(object sender, EventArgs e)
        {
            updatePic();
        }
    }
}
